package com.dinghui.ebookbag.module.middle;

import androidx.appcompat.app.AppCompatActivity;

import android.icu.util.LocaleData;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.enity.SpinnerItem;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SyncTestActivity extends AppCompatActivity implements View.OnClickListener {

    TextView leftBack;
    Spinner spSubject, spYear, spType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync_test);
        initView();
        initData();
    }

    private void initView() {
        leftBack = (TextView) findViewById(R.id.left_back);
        spSubject = (Spinner) findViewById(R.id.sp_subject);
        spYear = (Spinner) findViewById(R.id.sp_year);
        spType = (Spinner) findViewById(R.id.sp_type);
        leftBack.setOnClickListener(this);
    }

    private void initData() {
        showSubjectData();
        requestYearData();
        showTypeData();
    }

    // 学科
    private void showSubjectData() {
        List<SpinnerItem> list = new ArrayList<SpinnerItem>();
        list.add(new SpinnerItem("1",null, "语文"));
        list.add(new SpinnerItem("2",null, "数学"));
        list.add(new SpinnerItem("3",null, "英语"));
        list.add(new SpinnerItem("4",null, "物理"));
        list.add(new SpinnerItem("5",null, "化学"));
        list.add(new SpinnerItem("6",null, "生物"));
        list.add(new SpinnerItem("7",null, "政治"));
        list.add(new SpinnerItem("8",null, "历史"));
        list.add(new SpinnerItem("9",null, "地理"));

        ArrayAdapter<SpinnerItem> adapter= new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        spSubject.setAdapter(adapter);

        spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String subjectId = ((SpinnerItem)spSubject.getSelectedItem()).getID();

                Toast.makeText(SyncTestActivity.this, subjectId, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }

    // 年份
    private void requestYearData() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        List<String> yearList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            yearList.add(String.valueOf((year - i)));
        }
        ArrayAdapter<String> adapter= new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, yearList);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        spYear.setAdapter(adapter);
    }

    // 类别
    private void showTypeData() {
        List<SpinnerItem> list = new ArrayList<SpinnerItem>();
        list.add(new SpinnerItem("1",null, "期中考试"));
        list.add(new SpinnerItem("2",null, "期末考试"));
        list.add(new SpinnerItem("3",null, "中考真题"));
        list.add(new SpinnerItem("4",null, "高考真题"));
        list.add(new SpinnerItem("5",null, "模拟考试"));
        list.add(new SpinnerItem("6",null, "专题训练"));

        ArrayAdapter<SpinnerItem> adapter= new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        spType.setAdapter(adapter);

        spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String subjectId = ((SpinnerItem)spSubject.getSelectedItem()).getID();

                Toast.makeText(SyncTestActivity.this, subjectId, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_back:
                finish();
                break;
        }
    }
}